package bit.pja.wolfkill.Controller;


import bit.pja.wolfkill.Service.RoomService;
import bit.pja.wolfkill.Service.WolfService;
import bit.pja.wolfkill.entity.*;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/wolf")
@Api(tags ="狼人杀")
public class WolfController {

    private static final Logger logger = LoggerFactory.getLogger(WolfController.class);

//    @Autowired
//    private WolfService wolfService;

    @Autowired
    private RoomService roomService;


    @ApiOperation(value="创建房间",nickname="pja")
    @PostMapping("/create2/{userId}")
    public String create(@PathVariable("userId") Integer userId,
                         @RequestBody RolesParam rolesParam) {
        try {
            Room room = roomService.createRoom(userId, rolesParam);
            room = roomService.join(userId, room);
            int index = roomService.getUserIndex(room, userId);
            AjaxResult ajax = new AjaxResult();
            ajax.put("index", index);
            ajax.put("room", room);
            return JSON.toJSONString(AjaxResult.successData(ajax));
        }catch (Exception e) {
            logger.info(e.getMessage());
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }

    @ApiOperation(value="测试房间",nickname="pja")
    @PostMapping("/create/test")
    public String test(@RequestParam(value = "userId", required = false, defaultValue = "1") Integer userId,
                       @RequestParam(value = "roomId", required = false, defaultValue = "1")Integer roomId) {
        try {
            RolesParam rolesParam = new RolesParam(3, 3, true, true , true);
            Room room = roomService.createRoom(userId, rolesParam, roomId);
            for(int index=0; index < room.getSize(); index++) {
                room = roomService.join(userId+index, room);
            }
            AjaxResult ajaxResult =AjaxResult.successData(room);
            logger.info(String.format("%d号测试房间创建成功", roomId));
            return JSON.toJSONString(ajaxResult);
        }catch (Exception e) {
            logger.info(e.getMessage());
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }

    @ApiOperation(value="清空所有房间",nickname="pja")
    @GetMapping("/clear")
    public String clear() {
        roomService.clear();
        return "haha";
    }

    @ApiOperation(value="查询房间信息",nickname="pja")
    @GetMapping("/{roomId}")
    public String select( @PathVariable("roomId") Integer roomId) {
        try {
            Room room = roomService.getRoom(roomId);
            AjaxResult ajaxResult = AjaxResult.successData(room);
            return JSON.toJSONString(ajaxResult);

        } catch (Exception e) {
            logger.info(e.getMessage());
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }


    @ApiOperation(value="开始游戏",nickname="pja")
    @PostMapping("/{roomId}")
    public String begin(@RequestBody Map<String, Integer> map, @PathVariable("roomId") Integer roomId) {
        try {
            Integer userId = map.get("userId");
            Room room = roomService.begin(userId, roomId);
            logger.info(String.format("%d号房间开始游戏", roomId));
            AjaxResult ajaxResult = AjaxResult.successData(room);
            return JSON.toJSONString(ajaxResult);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }

    @ApiOperation(value="put房间，100进入，101退出",nickname="pja")
    @PutMapping("/{roomId}")

    public String put(@PathVariable("roomId") Integer roomId,PutParams params) {
        /*
        params.setFlag( map.get("flag"));
        params.setFromIndex(map.get("fromIndex"));
        params.setToIndex(map.get("toIndex"));
        params.setUserId( map.get("userId"));
        * */



        try {
            if (params.getFlag() == PutParams.JOIN) {
                Room room = roomService.join(params.getUserId(), roomId);
                int index = roomService.getUserIndex(room, params.getUserId());
                AjaxResult ajax = new AjaxResult();
                ajax.put("index", index);
                ajax.put("room", room);
                return JSON.toJSONString(AjaxResult.successData(ajax));
            }
            if (params.getFlag() == PutParams.QUIT) {
                Room room = roomService.quit(params.getUserId(), roomId);
                AjaxResult ajaxResult = AjaxResult.successData(room);
                return JSON.toJSONString(ajaxResult);
            }


            Room room = roomService.process(roomId, params);
            AjaxResult ajaxResult = AjaxResult.successData(room);
            return JSON.toJSONString(ajaxResult);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return JSON.toJSONString(AjaxResult.error(e.getMessage()));
        }
    }






}
